<template>
  <div class="register-container">
    <el-card class="register-card">
      <h1 class="register-title">Register</h1>
      <el-form :model="form" :rules="rules" ref="registerForm" label-width="100px" @keyup.enter="register">
        <el-form-item label="Username" prop="username">
          <el-input v-model="form.username" placeholder="Username" clearable></el-input>
        </el-form-item>
        <el-form-item label="Password" prop="password">
          <el-input type="password" v-model="form.password" placeholder="Password" show-password clearable></el-input>
        </el-form-item>
        <el-form-item label="Confirm" prop="confirmPassword">
          <el-input type="password" v-model="form.confirmPassword" placeholder="Confirm Password" show-password clearable></el-input>
        </el-form-item>
        <el-form-item label="Nickname" prop="nickname">
          <el-input v-model="form.nickname" placeholder="Nickname" clearable></el-input>
        </el-form-item>
        <el-form-item label="Email" prop="email">
          <el-input v-model="form.email" placeholder="Email (Optional)" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="register" class="register-button">Register</el-button>
          <div class="login-link">
        Already have an account? <router-link to="/login" >Login</router-link>
      </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'

const form = ref({
  username: '',
  password: '',
  confirmPassword: '',
  nickname: '',
  email: ''
})

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('Please input the password'))
  } else {
    if (form.value.confirmPassword !== '') {
      registerForm.value.validateField('confirmPassword')
    }
    callback()
  }
}

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('Please confirm the password'))
  } else if (value !== form.value.password) {
    callback(new Error('Two inputs don\'t match!'))
  } else {
    callback()
  }
}

const rules = ref({
  username: [
    { required: true, message: 'Please input username', trigger: 'blur' },
    { min: 6, message: 'Username must be at least 6 characters', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9]+$/, message: 'Username can only contain letters and numbers', trigger: 'blur' }
  ],
  password: [
    { required: true, validator: validatePass, trigger: 'blur' },
    { min: 8, message: 'Password must be at least 8 characters', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass2, trigger: 'blur' }
  ],
  nickname: [
    { required: true, message: 'Please input nickname', trigger: 'blur' },
    { min: 2, max: 20, message: 'Nickname must be between 2 and 20 characters', trigger: 'blur' }
  ],
  email: [
    { type: 'email', message: 'Please input correct email address', trigger: 'blur' }
  ]
})

const registerForm = ref(null)
const router = useRouter()
const authStore = useAuthStore()

const register = () => {
  registerForm.value.validate(async (valid) => {
    if (valid) {
      const response = await authStore.register({
        username: form.value.username,
        password: form.value.password,
        nickname: form.value.nickname,
        email: form.value.email
      })
      if (response.success) {
        ElMessage.success(response.message)
        // 使用 auth store 存储临时注册信息
        authStore.setTempRegisterInfo({
          username: form.value.username,
          password: form.value.password
        })
        router.push('/login')
      } else {
        ElMessage.error(response.message)
      }
    } else {
      ElMessage.error('Please fill the form correctly')
    }
  })
}
</script>

<style scoped>
.register-container {
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(to right, #6a11cb 0%, #2575fc 100%);
  animation: gradient 15s ease infinite;
}

@keyframes gradient {
  0% {
    background: linear-gradient(to right, #6a11cb 0%, #2575fc 100%);
  }
  50% {
    background: linear-gradient(to right, #2575fc 0%, #6a11cb 100%);
  }
  100% {
    background: linear-gradient(to right, #6a11cb 0%, #2575fc 100%);
  }
}

.register-card {
  width: 400px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  transition: all 0.3s;
  background-color: #fff;
}

.register-card:hover {
  box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.2);
}

.register-title {
  text-align: center;
  margin-bottom: 20px;
}

.el-input__inner {
  transition: all 0.3s;
}

.el-input__inner:focus {
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
  border-color: #409eff;
}

.register-button {
  width: 100%;
  background-color: #409eff;
  border-color: #409eff;
  transition: all 0.3s;
}

.register-button:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
  text-decoration: None;
}
.login-link {
  text-align: center;
  margin-top: 15px;
  text-decoration: None;
}
</style>